//
//  OverlayPresentationController.m
//  CustomModalTransition
//
//  Created by 朱志佳 on 2019/4/4.
//  Copyright © 2019 朱志佳. All rights reserved.
//

#import "OverlayPresentationController.h"

@implementation OverlayPresentationController

- (void)presentationTransitionWillBegin{
 
    _dimmingView = [[UIView alloc] init];
    [self.containerView addSubview:_dimmingView];
    CGFloat dimmingViewInitialWidth = self.containerView.frame.size.width * 2 / 3;
    CGFloat dimmingViewInitialHeight = self.containerView.frame.size.height * 2 / 3;
    _dimmingView.backgroundColor = [UIColor greenColor];
    _dimmingView.center = self.containerView.center;
    _dimmingView.bounds = CGRectMake(0, 0, dimmingViewInitialWidth, dimmingViewInitialHeight);
    
    __weak typeof(self) weakSelf = self;
    [self.presentedViewController.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        weakSelf.dimmingView.bounds = weakSelf.containerView.bounds;
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        
    }];
}
//- (void)presentationTransitionDidEnd:(BOOL)completed;
- (void)dismissalTransitionWillBegin{
    
    __weak typeof(self) weakSelf = self;
    [self.presentedViewController.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        weakSelf.dimmingView.alpha = 0.0;
        [weakSelf.dimmingView removeFromSuperview];
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        
    }];
    
}
//- (void)dismissalTransitionDidEnd:(BOOL)completed;

-(void)containerViewWillLayoutSubviews {
    
    _dimmingView.center = self.containerView.center;
    _dimmingView.bounds = self.containerView.bounds;
    
    CGFloat width = self.containerView.frame.size.width * 2 / 3;
    CGFloat height = self.containerView.frame.size.height * 2 / 3;
    self.presentedView.center = self.containerView.center;
    self.presentedView.bounds = CGRectMake(0, 0, width, height);
    
}

@end
